<!--

2023/02/17 luchuan
当前页面（分类节点）由productTempDescDetail、productFmlDescDetail产品页面引用当做子页面使用
在修改当前页面时，请慎重

-->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="产品分类" prop="qcProductCategory">
        <el-select v-model="queryParams.qcProductCategory" placeholder="请选择产品分类" style="width: 150px;" clearable size="small" @change="handleQuery">
          <el-option
            v-for="dict in configCategoryOptions"
            :key="dict.categoryCode"
            :label="dict.categoryName"
            :value="dict.categoryCode"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="国家代码" prop="countryCode" >
        <el-select v-model="queryParams.countryCode" placeholder="请选择国家" clearable size="small" style="width: 130px;" :disabled="!isVisibleProduct" @change="handleQuery" >
          <el-option
            v-for="dict in countryCodeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="节点状态" prop="nodeStatus" v-show="isVisibleProduct">
        <el-select v-model="queryParams.nodeStatus" placeholder="请选择节点状态" clearable size="small" @change="handleQuery" style="width: 130px;" >
          <el-option
            v-for="dict in nodeStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

<!--      <el-form-item label="商品类型" prop="feedProductType">
        <el-input
          style="width: 150px;"
          v-model="queryParams.feedProductType"
          placeholder="请输入商品类型(亚马逊分类节点名称属性)"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="节点 ID" prop="recommendedBrowseNode">
        <el-input
          style="width: 130px;"
          v-model="queryParams.recommendedBrowseNode"
          placeholder="请输入节点 ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="节点名称CN" prop="nodePathCn">
        <el-input
          v-model="queryParams.nodePathCn"
          placeholder="请输入节点节点名称中文"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['amz:configNode:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['amz:configNode:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['amz:configNode:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['amz:configNode:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="configNodeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" v-if="isVisibleProduct"/>
      <el-table-column label="序号" type="index" width="50" />
      <el-table-column label="产品分类" align="center" prop="qcProductCategory" width="100" :formatter="qcProductCategoryFormat" />
      <el-table-column label="国家" align="center" prop="countryCode" width="100" :formatter="countryCodeFormat" />
<!--
      <el-table-column label="商品类型" align="center" prop="feedProductType" width="80"/>
-->
      <el-table-column label="节点 ID" align="center" prop="recommendedBrowseNode" show-overflow-tooltip />
      <el-table-column label="节点路径EN" align="center" prop="nodePathEn" show-overflow-tooltip />
      <el-table-column label="节点路径CN" align="center" prop="nodePathCn" show-overflow-tooltip />
      <el-table-column label="状态" align="center" prop="nodeStatus" width="80" :formatter="nodeStatusFormat" v-if="isVisibleProduct"/>
      <!--<el-table-column label="节点排序" align="center" prop="nodeSort" />-->
      <el-table-column label="创建人" align="center" prop="createBy" width="80" v-if="isVisibleProduct" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="100" v-if="isVisibleProduct">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" width="100" v-if="isVisibleProduct" show-overflow-tooltip  />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-show="isVisibleProduct"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['amz:configNode:edit']"
          >修改</el-button>
          <el-button v-show="isVisibleProduct"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['amz:configNode:remove']"
          >删除</el-button>
          <el-button
            v-show="!isVisibleProduct"
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="handleChecked(scope.row)"
          >选中</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改分类节点维护对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="70%" :close-on-click-modal="false" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="产品分类" prop="qcProductCategory">
              <el-select v-model="form.qcProductCategory" placeholder="请选择产品分类" :disabled="form.id!=null">
                <el-option
                  v-for="dict in configCategoryOptions"
                  :key="dict.categoryCode"
                  :label="dict.categoryName"
                  :value="dict.categoryCode"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item label="国家代码" prop="countryCode">
              <el-select v-model="form.countryCode" placeholder="请选择国家代码" :disabled="form.id!=null">
                <el-option
                  v-for="dict in countryCodeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item label="节点状态" prop="nodeStatus">
              <el-select v-model="form.nodeStatus" placeholder="请选择节点状态">
                <el-option
                  v-for="dict in nodeStatusOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item label="节点排序" prop="nodeSort">
              <el-input v-model="form.nodeSort" placeholder="请输入节点排序" />
            </el-form-item>
          </el-col>

        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="商品类型" prop="feedProductType">
              <el-input v-model="form.feedProductType" disabled placeholder="请输入商品类型" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="节点 ID" prop="recommendedBrowseNode">
              <el-input v-model="form.recommendedBrowseNode" placeholder="请输入节点 ID" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="节点路径EN" prop="nodePathEn">
              <el-input v-model="form.nodePathEn" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="节点路径CN" prop="nodePathCn">
              <el-input v-model="form.nodePathCn" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>

        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listConfigNode, getConfigNode, delConfigNode, addConfigNode, updateConfigNode, exportConfigNode } from "@/api/amz/configNode";
import { getConfigCategoryByDept } from "@/api/amz/configCategory";

export default {
  name: "ConfigNode",
  components: {
  },
  props:['countryCode'],//设置props属性值，得到父组件传递过来的数据
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 分类节点维护表格数据
      configNodeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 产品类别列表-部门
      configCategoryOptions: [],
      // 国家代码字典
      countryCodeOptions: [],
      // 节点状态字典
      nodeStatusOptions: [],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        qcProductCategory: null,
        countryCode: null,
        feedProductType: null,
        recommendedBrowseNode: null,
        nodeStatus: null,
        nodePathCn: null,
      },
      // 表单参数
      form: {},

      isVisibleProduct:true,
      // 表单校验
      rules: {
        qcProductCategory: [
          { required: true, message: "产品分类不能为空", trigger: "change" }
        ],
        countryCode: [
          { required: true, message: "国家代码不能为空", trigger: "change" }
        ],
        /*feedProductType: [
          { required: true, message: "商品类型不能为空", trigger: "blur" }
        ],*/
        recommendedBrowseNode: [
          { required: true, message: "节点 ID不能为空", trigger: "blur" }
        ],
        nodeStatus: [
          { required: true, message: "节点状态不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {

    if (this.countryCode){
      this.queryParams.countryCode = this.countryCode;
      this.isVisibleProduct = false;
    }

    /* 获取数据信息*/
    this.getList();

    /* 获取类目配置-部门*/
    this.getConfigCategoryByDept();

    this.getDicts("qc_country_code").then(response => {
      this.countryCodeOptions = response.data;
    });
    this.getDicts("qc_status").then(response => {
      this.nodeStatusOptions = response.data;
    });

  },
  methods: {
    /** 查询分类节点维护列表 */
    getList() {
      this.loading = true;
      listConfigNode(this.queryParams).then(response => {
        this.configNodeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /* 获取类目配置-部门*/
    getConfigCategoryByDept(){
      getConfigCategoryByDept().then(response => {
        this.configCategoryOptions = response.data;
      });
    },
    // 产品分类字典翻译
    qcProductCategoryFormat(row, column) {
      var categoryName = "";
      this.configCategoryOptions.forEach(obj =>{
        if (obj.categoryCode===row.qcProductCategory){
          categoryName = obj.categoryName;
        }
      });
      return categoryName;
    },
    // 国家代码字典翻译
    countryCodeFormat(row, column) {
      return this.selectDictLabel(this.countryCodeOptions, row.countryCode);
    },
    // 节点状态字典翻译
    nodeStatusFormat(row, column) {
      return this.selectDictLabel(this.nodeStatusOptions, row.nodeStatus);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        qcProductCategory: null,
        countryCode: null,
        feedProductType: null,
        recommendedBrowseNode: null,
        nodePathEn: null,
        nodePathCn: null,
        nodeStatus: null,
        nodeSort: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加分类节点";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getConfigNode(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改分类节点";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateConfigNode(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addConfigNode(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除分类节点维护编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delConfigNode(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有分类节点维护数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportConfigNode(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },

    /**
     * 当前选中方法由，产品页面选择分类节点时，选中事件
     * 选中后，由子页面通过this.$emit调用父页面方法，进行赋值，同时关闭弹框
     * @param row
     */
    handleChecked(row) {
      this.$emit('configNodeDialogParent',row.recommendedBrowseNode);
    },

  }
};
</script>
